package com.example.demo.service;

import com.example.demo.model.Response;
import com.example.demo.model.User;
import com.example.demo.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据userId查询是否注册
     * @param userId
     * @return
     */
    public Boolean checkUserByUserId(String userId) {
        return jdbcTemplate.queryForObject("select * from dl_user where userId=" + userId, new RowMapper<Boolean>() {
            @Override
            public Boolean mapRow(ResultSet resultSet, int i) throws SQLException {
                if (resultSet.getString("userId").equals(userId)) {
                    return true;
                }
                return false;
            }
        });
    }

    /**
     * 根据userName查询是否注册
     * @param userName
     * @return
     */
    public Boolean checkUserByUserName(String userName) {
        try {
            return jdbcTemplate.queryForObject("select * from dl_user where userName=" + userName, new RowMapper<Boolean>() {
                @Override
                public Boolean mapRow(ResultSet resultSet, int i) throws SQLException {
                    return true;
                }
            });
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * 注册帐号
     * @param userName
     * @param password
     * @return
     */
    public boolean registorUserAction(String userName, String password) {
        boolean isreg = checkUserByUserName(userName);
        if (isreg) {
            return false;
        }
        int rows = jdbcTemplate.update("insert into dl_user values(?,?,?)", Util.getUUID(), userName, password);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    /**
     * 帐号密码登录
     * @param userName
     * @param password
     * @return
     */
    public Response loginByUserNameAction(String userName, String password) {
        String sql = "select * from dl_user where userName="+userName;
        return jdbcTemplate.queryForObject(sql, new RowMapper<Response>() {
            @Override
            public Response mapRow(ResultSet resultSet, int i) throws SQLException {
                Response response = new Response();
                if (resultSet.getString("userName").equals(userName) && resultSet.getString("password").equals(password)) {
                    response.setStatus(1);
                    response.setData(resultSet.getString("userId"));
                }
                else {
                    response.setStatus(0);
                    response.setErrCode("1000");
                    response.setErrMsg("帐号或密码错误");
                    response.setData(null);
                }
                return response;
            }
        });
    }

    /**
     * 更新用户信息
     * @param userId
     * @param phone
     * @param password
     * @return
     */
    public boolean updateUserInfoAction(String userId, String phone, String password) {
        String sql = "update dl_user set userName=?,password=? where userId=?";
        int rows = jdbcTemplate.update(sql, phone, password, userId);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询用户列表
     * @return
     */
    public List<User> getUserList() {
        return jdbcTemplate.query("select * from dl_user", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setUserId(resultSet.getString("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        });
    }

}
